package cn.swpu.blog.controller;

import cn.swpu.blog.from.SaveBlogFrom;
import cn.swpu.blog.from.UpdateBlogFrom;
import cn.swpu.blog.service.IBlogService;
import cn.swpu.blog.vo.BlogDetailVo;
import cn.swpu.blog.vo.ResponseVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author 冰翔
 * @Date Created in 11:29 上午 2020/12/11
 */
@Api(tags = "博客Blog")
@RequestMapping("/blog")
@RestController
public class BlogController {

    @Autowired
    private IBlogService blogService;

    @PreAuthorize("permitAll()")
    @ApiOperation("在URL设置blogId获取，博客的所有信息")
    @PostMapping("/detail/{blogId}")
    public ResponseVo<BlogDetailVo> detail(@PathVariable Integer blogId) {
        return blogService.queryBlogDetail(blogId);
    }

    @PreAuthorize("permitAll()")
    @ApiOperation("分页返回所有博文，默认pageNum=1, pageSize=10")
    @PostMapping("/allBlog")
    public ResponseVo<PageInfo> pageBlog(@RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize){
        return blogService.pageBlog(pageNum, pageSize);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("存储博客")
    @PostMapping("/saveBlog")
    public ResponseVo<Map<String, Integer>> saveBlog(@RequestBody SaveBlogFrom from) {
        return blogService.saveBlog(from);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("置顶博客")
    @GetMapping("/setTop/{blogId}")
    public ResponseVo setTop(@PathVariable Integer blogId) {
        return blogService.setTop(blogId);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("取消置顶")
    @GetMapping("/cancelTop/{blogId}")
    public ResponseVo cancel(@PathVariable Integer blogId) {
        return blogService.cancelTop(blogId);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("删除博客")
    @DeleteMapping("/deleteBlog/{blogId}")
    public ResponseVo delete(@PathVariable Integer blogId) {
        return blogService.deleteBlog(blogId);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("更新博客")
    @PostMapping("/updateBlog")
    public ResponseVo updateBlog(@RequestBody UpdateBlogFrom from) {
        return blogService.updateBlog(from);
    }
}
